package com.example.fish.controller;

import com.example.fish.pojo.Gear;
import com.example.fish.pojo.User;
import com.example.fish.service.GearService;
import com.example.fish.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/gear")
public class GearController extends BaseController{

    @Autowired
    private GearService gearService;

    /***
     * 返回渔具信息
     * @return list集合
     */
    @GetMapping("/info")
    public JsonResult<List<Gear>> showGearInfo(HttpSession session){
        User user =(User) session.getAttribute("user");
        List<Gear> gearList = gearService.selectGearsByUserId(user.getUserid());
        return new JsonResult<>(200,"success",gearList);
    }

    /***
     * 保存渔具信息
     * @param gear 渔具对象
     * @return int
     */
    @PostMapping("/add")
    public JsonResult<Integer> addGear(Gear gear,HttpSession session){
        User user =(User) session.getAttribute("user");
        Integer integer = gearService.insertGear(user.getUserid(), gear);

        return new JsonResult<>(200,"success",integer);
    }

    /***
     *  删除渔具信息
     * @param gearid 渔具id
     * @return int
     */
    @PostMapping("/delete")
    public JsonResult<Integer> deleteGear(@RequestParam("gearid") Integer gearid){
        Integer integer = gearService.deleteGear(gearid);
        return new JsonResult<>(200,"success",integer);
    }

    @PostMapping("/update")
    public JsonResult<Integer> updateGear(Gear gear){
        System.out.println("update"+gear.getBuytime());
        Integer integer = gearService.updateGear(gear);
        return new JsonResult<>(200,"success",integer);
    }
}
